package com.bbzn.device.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.service.SystemParamService;
import com.bbzn.device.client.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * Created by 黄志鹏
 * 2018-03-04 21:45
 */
@Service
@Slf4j
public class SystemParamByCacheServiceimpl implements SystemParamByCacheService {

    @Autowired
    private RedisUtils<String> redisUtil;

    @Autowired
    private SystemParamService systemParamService;

    @Override
    public SystemParam getValues(String type, String key) throws Exception {
        boolean hExists = redisUtil.hExists(type, key);
        SystemParam systemParam = null;
        if (hExists) {
            String json = redisUtil.hGet(type, key);
            systemParam = JSON.parseObject(json, SystemParam.class);
            redisUtil.sAdd("SystemEnum",type+"_"+key);
        } else {
            systemParam = new SystemParam();
            systemParam.setKey(key);
            systemParam.setType(type);
            List<SystemParam> systemParams = systemParamService.selectSelective(systemParam);
            if (CollectionUtils.isEmpty(systemParams)) {
                systemParam.setMassage("出现不可预料的异常，没有查询到该系统参数");
                return systemParam;
            }
            systemParam = systemParams.get(0);
        }
        if (null == systemParam) {
            systemParam = new SystemParam();
            systemParam.setCode(1);
            systemParam.setMassage("出现不可预料的异常，没有查询到该系统参数");
            return systemParam;
        } else {
            systemParam.setCode(Integer.parseInt(systemParam.getValue()));
            redisUtil.hPut(type, key, JSON.toJSONString(systemParam));
            redisUtil.sAdd("SystemEnum",type+"_"+key);
            return systemParam;
        }
    }

    @Override
    public List<SystemParam> getValues(String type) throws Exception {
        List<String> jsons = redisUtil.hValues(type);
        List<SystemParam> systemParams = null;
        if (CollectionUtils.isNotEmpty(jsons)) {
            systemParams = jsons.stream().map(json -> {
                return JSON.parseObject(json, SystemParam.class);
            }).collect(Collectors.toList());
        } else {
            SystemParam systemParam = new SystemParam();
            systemParam.setType(type);
            systemParams = systemParamService.selectSelective(systemParam);
        }
        if (CollectionUtils.isNotEmpty(systemParams)) {
            Map<String, String> hmap = systemParams.stream().collect(Collectors.toMap(SystemParam::getKey, t -> JSON.toJSONString(t)));
            redisUtil.hPutAll(type, hmap);
            return systemParams;
        }
        return null;
    }


    @Override
    public SystemParam getValue(String type, String key) throws Exception {
        boolean hExists = redisUtil.hExists(type, key);
        SystemParam systemParam = null;
        ;
        if (hExists) {
            String json = redisUtil.hGet(type, key);
            systemParam = JSON.parseObject(json, SystemParam.class);
        } else {
            systemParam = new SystemParam();
            systemParam.setKey(key);
            systemParam.setType(type);
            List<SystemParam> systemParams = systemParamService.selectSelective(systemParam);
            if (CollectionUtils.isEmpty(systemParams)) {
                systemParam.setMassage("出现不可预料的异常，没有查询到该系统参数");
                return systemParam;
            }
            systemParam = systemParams.get(0);
        }
        if (null == systemParam) {
            systemParam = new SystemParam();
            systemParam.setCode(1);
            systemParam.setMassage("出现不可预料的异常，没有查询到该系统参数");
            return systemParam;
        } else {
            redisUtil.hPut(type, key, JSON.toJSONString(systemParam));
            redisUtil.sAdd("SystemEnum",type+"_"+key);
            return systemParam;
        }
    }
}
